package com.android.infantschool.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.app.InfantApp;
import com.android.infantschool.bean.BabyClassPhotoBean;
import com.android.infantschool.bean.TelBookBean;
import com.android.infantschool.ui.imageloader.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class TelPhoneAdapter extends RecyclerView.Adapter<TelPhoneAdapter.MyHolder> {

    private Context mContext;
    public List<TelBookBean.DataBean> list = null;
    private OnLister lister;

    public TelPhoneAdapter(Context context, OnLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_tel_book, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position) {
        TelBookBean.DataBean bean = list.get(position);

        Glide.with(mContext).load(bean.img)
                .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext))
                .into(viewHolder.imgIcon);
        viewHolder.tvName.setText(bean.babyNameAndRole);
        viewHolder.tvPhone.setText(bean.phone);

        viewHolder.imgTel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lister != null) {
                    lister.lister(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<TelBookBean.DataBean> list) {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<TelBookBean.DataBean> list) {
        this.list.addAll(0, list);
        this.notifyDataSetChanged();
    }

    /**
     * 加载添加数据
     *
     * @param list
     */
    public void addDataAll(List<TelBookBean.DataBean> list) {
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public ImageView imgIcon;
        public ImageView imgTel;
        public TextView tvName;
        public TextView tvPhone;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgIcon = itemView.findViewById(R.id.img_icon);
            imgTel = itemView.findViewById(R.id.img_tel);
            tvName = itemView.findViewById(R.id.tv_name);
            tvPhone = itemView.findViewById(R.id.tv_tel_phone);
        }
    }

    public interface OnLister {
        void lister(int position);
    }
}